package com.alibaba.aliper.server.model.domain;

import java.awt.Color;
import java.io.IOException;
import java.util.HashMap;

import com.alibaba.aliper.core.chart.AliperChart;
import com.alibaba.aliper.server.model.interfaces.ChartControll;
import com.alibaba.aliper.server.model.interfaces.ChartView;
import com.alibaba.aliper.server.model.interfaces.LineView;

public class Chart implements ChartView , ChartControll{
	public Chart(String chartId){
		this.id = chartId;
		this.aliperChart = new AliperChart(chartId, "阿里巴巴平台测试开发部", Color.LIGHT_GRAY);
	}
	private String id;
	public String getId(){
		return this.id;
	}
	private AliperChart aliperChart = null;
	private byte[] imageCache = null;
	public byte[] getImage() throws IOException {
		if(imageCache == null){
		    imageCache = aliperChart.getImageData(600, 400);
		}
	    return imageCache;
	}

	
	public void report(String lineId, long time, double data) {
		//this.aliperChart.getLine(lineId, Color.blue, false).fill(time, data);
		this.imageCache = null;
		this.getLineOnCreate(lineId).addPoint(time,data);
	}




	@Override
	public LineView[] getLines() {
		// TODO Auto-generated method stub
		return this.lines.values().toArray(new LineView[this.lines.size()]);
	}

	private HashMap<String,Line> lines = new HashMap<String,Line>();
	@Override
	public LineView getLine(String lineId) {
		return lines.get(lineId);
	}
	public Line getLineOnCreate(String lineId) {
		Line line = null;
		if(lines.containsKey(lineId)){
			line = lines.get(lineId);
		} else {
			line = new Line(lineId);
			lines.put(lineId, line);
		}
		return line;
	}


	@Override
	public void setRante(String lineId, double min, double max) {
		this.getLineOnCreate(lineId).setRange(min, max);
	}


	public void restart() {
		this.lines.clear();
	}

}
